Write-Host "🚀 启动AI头像生成后端服务..." -ForegroundColor Green
Write-Host ""

Set-Location backend

Write-Host "📦 检查依赖..." -ForegroundColor Yellow
if (-not (Test-Path "node_modules")) {
    Write-Host "正在安装依赖..." -ForegroundColor Cyan
    npm install
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ 依赖安装失败" -ForegroundColor Red
        Read-Host "按回车键退出"
        exit 1
    }
    Write-Host "✅ 依赖安装完成" -ForegroundColor Green
} else {
    Write-Host "✅ 依赖已存在" -ForegroundColor Green
}

Write-Host ""
Write-Host "🔑 检查环境配置..." -ForegroundColor Yellow
if (-not (Test-Path ".env")) {
    Write-Host "正在创建环境配置文件..." -ForegroundColor Cyan
    Copy-Item "env.example" ".env"
    Write-Host ""
    Write-Host "⚠️  请编辑 .env 文件，配置你的AI API密钥：" -ForegroundColor Yellow
    Write-Host "     - STABLE_DIFFUSION_API_KEY: 你的Stability AI API密钥" -ForegroundColor White
    Write-Host "     - OPENAI_API_KEY: 你的OpenAI API密钥" -ForegroundColor White
    Write-Host ""
    Write-Host "如果没有API密钥，服务仍会运行，但会使用预设模板" -ForegroundColor Cyan
    Write-Host ""
    Read-Host "按回车键继续"
}

Write-Host ""
Write-Host "🎯 启动后端服务..." -ForegroundColor Green
Write-Host "📱 前端地址: http://localhost:5173" -ForegroundColor Cyan
Write-Host "🔗 后端地址: http://localhost:3001" -ForegroundColor Cyan
Write-Host "📊 健康检查: http://localhost:3001/health" -ForegroundColor Cyan
Write-Host ""
Write-Host "按 Ctrl+C 停止服务" -ForegroundColor Yellow
Write-Host ""

npm run dev
